package yl.hs.bmipfull.service.laboratorydata.imple;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigDesignDetailDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigDesignRecordDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigSpecimenDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabConfigDesignDetailEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabConfigDesignRecordEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabConfigSpecimenEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigDesignRecordService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.LaboratoryDataState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 试配记录-试配基础信息服务层接口实现
 * @since 2022年07月06日 09时20分03秒 <br/>
 */
@Service
public class LabConfigDesignRecordServiceImpl extends LaboratorydataBaseService implements ILabConfigDesignRecordService {

    @Autowired
    ILabConfigDesignRecordDao dao;
    @Autowired
    ILabConfigDesignDetailDao detailDao;
    @Autowired
    ILabConfigSpecimenDao specimenDao;

    @Override
    public List<LabConfigDesignRecordEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabConfigDesignRecordEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<LabConfigDesignRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabConfigDesignRecordEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabConfigDesignRecordEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public LabConfigDesignRecordEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabConfigDesignRecord(Map<String, Object> model, String sn, List<LabConfigDesignDetailEntity> dates) throws Exception {
        Integer result = 0;
        LabConfigDesignRecordEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabConfigDesignRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabConfigDesignRecordEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                //插入子表
                insertLabConfigDesignDetail(dates,entity.getSn());
                //添加试件信息
                if(entity.getGroupNumber()>0){
                    LabConfigSpecimenEntity specimenEntity=new LabConfigSpecimenEntity();
                    for (int i=0;i<entity.getGroupNumber();i++){
                        specimenEntity.setConfigSN(entity.getConfigSN());
                        specimenEntity.setConfigRecordSN(entity.getSn());
                        specimenEntity.setProductSN(entity.getProductSN());
                        specimenEntity.setItemCXRQ(new Date());
                        specimenEntity.setItemSize(entity.getEachGroupNumber());
                        specimenEntity.setItemYHSD(entity.getItemSDKZ());
                        specimenEntity.setItemYHWD(entity.getItemWDKZ());
                        specimenEntity.setSn(queryNewSN(LabConfigSpecimenEntity.class));
                        specimenDao.insertNew(specimenEntity);
                        writeLog(ActionType.CREATE, specimenEntity.getSn(), "插入试件信息-试配试件信息", Tl.toJson(specimenEntity), LabConfigSpecimenEntity.class);
                    }
                }
                writeLog(ActionType.CREATE, entity.getSn(), "插入试配记录-试配基础信息信息", Tl.toJson(entity), LabConfigDesignRecordEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                //插入子表
                insertLabConfigDesignDetail(dates,entity.getSn());
                writeLog(ActionType.UPDATE, sn, "更新试配记录-试配基础信息信息", Tl.toJson(entity), LabConfigDesignRecordEntity.class);
            }
        }

        return result;
    }

    private Integer insertLabConfigDesignDetail(List<LabConfigDesignDetailEntity> dates,String designSN){
        detailDao.deleteByDesignSN(designSN);
        var result=0;
        for (var entity: dates) {
            entity.setDesignSN(designSN);
            result+=detailDao.insertNew(entity);
        }
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabConfigDesignRecordEntity.class);
        }
        return result;
    }

    @Override
    public Integer changeEnableBySN(String sn, boolean isEnable) {
        var entity=dao.findBySN(sn);
        if(isEnable){
            entity.setItemQYRQ(new Date());
            entity.setIsEnable(true);
        } else{
            entity.setItemJYRQ(new Date());
            entity.setIsEnable(false);
        }
        return dao.update(entity);
    }
}